﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
   public class ActivitiesController : Controller
    {
       private readonly IActivitiesBiz _activitiesService;

       public ActivitiesController(IActivitiesBiz activitiesService)
        {
            this._activitiesService = activitiesService;
        }

       [ChildActionOnly]
       [OutputCache(Duration = 3600)]
       public PartialViewResult _TopFive()
       {
           var totalRecord = 0;
           var topFiveNews = _activitiesService.GetItemsByPages(out totalRecord, 1, 5, "");
           return PartialView("_TopFive", topFiveNews);
       }

       public ActionResult List(int? page = 1)
       {

           var size = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["pageSize"]);

           var totalRecord = 0;
           var activities = _activitiesService.GetItemsByPages(out totalRecord, pageIndex: page.GetValueOrDefault(), pageSize: size, orderFiled: "");

           var item = new PageItem()
           {
               CurrentPage = page.GetValueOrDefault(),
               TotalRecords = totalRecord,
               TotalPages = (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
               ControllerName = "Activities",
               ActionName = "List"
           };

           var viewModels = new Pager<Activities>() { DataSource = activities, Item = item };

           return View(viewModels);
       }

       public ActionResult Detail(int? id = 1)
       {
           var newsItem = _activitiesService.GetById(id.GetValueOrDefault());
           newsItem.ReadTimes++;
           _activitiesService.Update(newsItem);

           return View(newsItem);
       }

       ////[HttpPost]
       //public JsonResult SetReadTimes(int? id = 1, int? readTime = 1)
       //{
       //    var newsItem = _activitiesService.SetReadTimes(id.GetValueOrDefault(), readTime.GetValueOrDefault());
       //    //var json = new JsonResult() { Data = SerializationHelper.SerializeToJson<Boolean>(newsItem), JsonRequestBehavior = JsonRequestBehavior.AllowGet };

       //    return Json(newsItem, JsonRequestBehavior.AllowGet);
       //}

       public ActionResult Add()
       {
           return View();
       }

       public ActionResult Edit(int? id = 1)
       {
           var newsItem = _activitiesService.GetById(id.GetValueOrDefault());
           return View(newsItem);
       }

       [HttpPost]
       [ValidateInput(false)]
       public RedirectToRouteResult Edit(Activities item)
       {
           if (ModelState.IsValid)
           {
               item.CreatedTime = DateTime.Now;
               _activitiesService.Update(item);
           }
           return RedirectToAction("Detail", new { id = item.Id });
       }

       [HttpPost]
       [ValidateInput(false)]
       public ActionResult Add(Activities item)
       {
           if (ModelState.IsValid)
           {
               item.CreatedTime = DateTime.Now;
               item.ReadTimes = 1;
               _activitiesService.Create(item);
           }
           return View();
       }
    }
}
